package com.example.demo1;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.SneakyThrows;

public class Producer {
    @SneakyThrows
    public static void main(String[] args) {
        //创建工厂
        ConnectionFactory factory = new ConnectionFactory();
        //服务地址
        factory.setHost("192.168.1.3");
        //虚拟机
        factory.setVirtualHost("iotVirtual");
        //账号
        factory.setUsername("remote");
        //密码
        factory.setPassword("remote");
        //端口
        factory.setPort(5673);
        //创建连接
        Connection connection = factory.newConnection();
        //创建信道
        Channel channel = connection.createChannel();


        channel.exchangeDeclare("exchange_name", BuiltinExchangeType.DIRECT,true,false,null);

        //生成队列
        //1、队列名称
        //2、队列是否需要持久化
        //3、队列是否独占
        //4、是否自动删除
        //5、队列的其他信息 死信队列 绑定死信交换机
        channel.queueDeclare("queue_name2",true,false,false,null);


        //绑定队列到交换机
        //1、队列名称
        //2、交换机名称
        //3、路由键
        channel.queueBind("queue_name2","exchange_name","queue_name2");

        //发送消息
        String message = "hello rabbitmq";

        for (int i = 0; i < 10; i++){
            message = message + i;
            //发送消息
            //1、交换机名称
            //2、路由键名
            //3、消息的其他信息
            //4、消息主体
            channel.basicPublish("exchange_name","queue_name2",null,message.getBytes());
        }



        //关闭连接
        channel.close();
        connection.close();

        System.out.println("消息发送成功");
    }
}
